var pageNum;
$(function () {

    initLayerDate("#startDate",true);
    queryAjaxFeedBackList(1);
    $('body').on('click','#submitFrom,#querySubmit',function () {
        //点击搜索查询从第一页开始查询
        queryAjaxFeedBackList(1);
    });
    bonusPoints();
    //单个删除
    $('body').on('click','.deleteFeedBack',function () {
        var feedBackIds = $(this).data('id');
        var obj = $(this);
        icModale(2,"确认要删除所选反馈么？",3,function () {
            deleteFeedback(feedBackIds,obj);
        });
    });
    //批量删除
    $("body").on("click","#deleteBatch",function () {
        var obj = $('td input.i-checks:checked');
        var feedBackIds = obtainIds(obj);//获取勾选ids
        if(!$.isEmptyObject(feedBackIds)){
            icModale(2,"确认要删除所选反馈么？",3,function () {
                deleteFeedback(feedBackIds,obj);
            });
        }
    });
    //判断勾选ID是否为空
    function obtainIds(obj) {
        if (obj.size() == 0) {
            icModale(0,"请选择要批量操作的数据");
            return null;
        } else {
            var feedBackIds = "";
            obj.each(function (index, em) {
                feedBackIds += em.value + ",";
            });
            feedBackIds = feedBackIds.substr(0, feedBackIds.length - 1);
            return feedBackIds;
        }
    }

    /**
     * 批量删除
     * @param userIds
     * @param obj
     * @param isavalible
     */
    function deleteFeedback(feedBackIds,obj) {
        $.ajax({
            url: baseloaction + "/admin/op/feedback/deleteFeedback",
            data: {"feedbackIds": feedBackIds},
            type: "post",
            dataType: "json",
            success: function (result) {
                icModale(0, result.message);
                if (result.success==true) {
                    obj.parents('tr').remove();
                    var feedBackCount = $("#feedBackCount").html();
                    $("#feedBackCount").html((parseInt(feedBackCount) - obj.size()));
                    queryAjaxFeedBackList(pageNum);
                }
            },
            error: function (error) {
                icModale(0, "系统繁忙,请稍后重试");
            }
        })
    }

    /**
     * 获取角色列表页面
     * @param pageNo 当前页码
     */
    function queryAjaxFeedBackList(pageNo) {
        var params = $("#search-form").serializeJSON();
        params = JSON.stringify(params);
        var obj = {
            url: baseloaction + '/admin/op/feedback/ajaxQueryFeedbackList',//请求URL
            contextId: 'feedback-list-context',//响应内容页面设置内容ID
            params: params,//请求参数
            pageNo: pageNo,//页码
            pageCall: queryAjaxFeedBackList, //分页回调加载数据(点击上下页时调用,该方法也在加载页面完成时调用)
            selectAllId: 'select-all-feedBack', //全选框ID
            callback: function (pageNo) {//数据加载完成回调
                pageNum = pageNo;
                $("#feedBackCount").html(total);
            }
        };
        ajaxGetListData(obj)
    }

    /**
     * 回复、查看用户反馈
     */
    function bonusPoints() {
        $('body').on('click', '.updateFeedBack', function () {
            var dataId = $(this).data('id');
            var index = parent.layer.open({
                type: 2,
                shadeClose: false,
                shade: [0.3, '#000000'],
                title: '<strong>反馈信息</strong>',
                area: winArea,
                content: baseloaction + "/admin/op/feedback/toSaveUpdateFeedback?feedbackId="+dataId,
                btn: ['确定', '取消'],
                yes: function (index, layero) {
                    var replyContext = parent.layer.getChildFrame('body', index).find('#feedBack_replyContext').val();
                    var params = {"id":dataId,"replyContext":replyContext};
                    params = JSON.stringify(params);
                    $.ajax({
                        url:baseloaction + "/admin/op/feedback/saveUpdateFeedback",
                        type:"post",
                        data:params,
                        dataType:"json",
                        contentType: 'application/json;charset=utf-8',
                        success:function (result) {
                            icModale(0, result.message);
                            if (result.success == true) {
                                $('tr[data-id=' + dataId +'] td[data-status]').html('<span class="label label-info">已回复</span>');
                                $("#feedBack_replyContext_" + dataId).val(replyContext);
                                parent.layer.close(index);
                            }
                        },
                        error:function (error) {
                            parent.layer.close(index);
                            icModale(0,'系统繁忙，请稍后再试');
                        }
                    });
                }
            });
        }).on('click','.infoFeedBack',function () {
            var feedbackId = $(this).data('id');
            var index = parent.layer.open({
                type: 2,
                shadeClose: false,
                shade: [0.3, '#000000'],
                title: '<strong>查看反馈信息</strong>',
                area: winArea,
                content: baseloaction + "/admin/op/feedback/showFeedbackInfo?feedbackId="+feedbackId,
                btn: ['关闭'],
                yes: function (index, layero) {
                    parent.layer.close(index);
                }
            });
        });
    }
});
